/**
 * Created by Administrator on 2018/11/2 0002.
 */
var model_phone='';
//用户信息替换
function user_info_tihuan() {
    layui.use(['layer'], function() {
        var index = layer.load(2, {shade: [0.1,'#fff'],time: 60*1000});
        var id = $('#info_id').val();
        var html ='';
        sign.params={
            id:id
        };
        var encode_data = sign.encode_post();
        $.post(url_list.user_info,encode_data,function (data) {
            //解码
            if(typeof data == 'string'){
                data = JSON.parse(data);
            }
            data = sign.decode_post(data);

            if (data.code == 200) {
                model_phone=data.data.phone;
                html +='<li>'+
                    '<label class="res-lab">我的头像</label>'+
                    '<img id="update_img" width="100px" height="100px" src="/'+data.data.img+'">'+
                    '<a href="#" onclick="update_img()" style="color: #00b3ee">修改头像</a>'+
                    '</li>'+
                    '<li>'+
                    '<label class="res-lab">个人账号</label><span class="res-info" >'+data.data.username+'</span>'+
                '</li>'+
                '<li>'+
                '<label class="res-lab">真实姓名</label><span class="res-info" id="r_name">'+data.data.realname+'</span>'+
                '</li>'+
                '<li>'+
                '<label class="res-lab">手机号码</label><span class="res-info" id="r_phone">'+data.data.phone+'</span>'+
                '</li>'+
                '<li>'+
                '<label class="res-lab">邮箱地址</label><span class="res-info" id="r_email">'+data.data.email+'</span>'+
                '</li>'+
                '<li>'+
                '<label class="res-lab">个人简介</label><span class="res-info" id="r_intro">'+data.data.introduce+'</span>'+
                '</li>'+
                '<li>'+
                '<label class="res-lab">修改时间</label>'+
                    '<span class="res-info">'+data.data.updatetime+'</span>'+
                '</li>'+
                '<li>'+
                '<label class="res-lab"></label>'+
                    '<span class="res-info">'+
                    '<a href="#" style="color: #00b3ee" onclick="update_uer_info()">修改资料</a> '+
                    '</span>'+
                    '</li>';
                $('#sys-info-list').html(html);
            } else {
                layer.msg(data.msg, {icon: 2});
            }layer.close(index);
        })
    });
}

// 上传图片的弹窗，获取弹窗
var modal = document.getElementById('myModal');
// 打开弹窗的按钮对象
var btn = document.getElementById("myBtn");
// 获取 <span> 元素，用于关闭弹窗
var span = document.querySelector('.close');
//修改头像
function update_img() {
    modal.style.display = "block";
}
// 点击 <span> (x), 关闭弹窗
function update_img_close() {
    modal.style.display = "none";
};
// 在用户点击其他地方时，关闭弹窗
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
};

//个人信息修改
function info_update(img) {
    var realname = $('#realname').val(),
        email = $('#email').val(),
        introduce = $('#intro').val();
    sign.params={
        realname:realname,
        phone:model_phone,
        email:email,
        introduce:introduce,
        img:img
    };
    var encode_data = sign.encode_post();
    $.post(url_list.user_update_info,encode_data,function (data) {
        //解码
        if(typeof data == 'string'){
            data = JSON.parse(data);
        }
        data = sign.decode_post(data);
        console.log(data);
        layui.use(['layer'], function() {
            if (data.code == 200) {
                layer.msg(data.msg, {icon: 1, time: 1500});
                user_info_tihuan();
                outcli();
            } else {
                layer.msg(data.msg, {icon: 2});
            }
        })
    });
}
//上传图片，调用上传图片的接口
layui.use('upload', function(){
    var upload = layui.upload;
    //执行实例
    upload.render({
        elem: '#test1' //绑定元素
        ,url:url_list.upload //上传接口
        ,data:{'token':'FFFX123456'}
        ,done: function(res){
            //上传完毕回调
            console.log(res);

            layer.msg(res.msg, {icon: 1, time: 1500});
            // document.getElementById("update_img").src='/'+res.data.savepath;
            info_update(res.data.savepath);
            update_img_close();
        }
        ,error: function(){
            //请求异常回调
            layer.msg(res.msg, {icon: 2, time: 1500});
        }
    });
});

//修改弹出层用户信息，打开修改资料弹窗
function update_uer_info() {
    var name = $('#r_name').text(),
        // phone = $('#r_phone').text(),
        email = $('#r_email').text(),
        introduce = $('#r_intro').text();

    document.getElementById('realname').value = name;
    // document.getElementById('phone').value = phone;
    document.getElementById('email').value = email;
    document.getElementById('intro').value = introduce;
    $('.bgPop,.pop').show();
}
//关闭修改资料弹窗
function outcli() {
    $('.bgPop,.pop').hide();
}